/*
 * Copyright (C) 2015-2016  Nicola Spanti (RyDroid) <dev@nicola-spanti.info>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */


#include "PlanetWars2dRT-core/specific/controller/universe_collisions.h"
#include "PlanetWars2dRT-core/specific/controller/ship_collision.h"
#include "PlanetWars2dRT-core/specific/controller/planet_collision.h"
#include "PlanetWars2dRT-core/specific/model/universe_ships.h"
#include "PlanetWars2dRT-core/specific/model/universe_planets.h"
#include "PlanetWars2dRT-core/specific/model/planet_position.h"
#include "PlanetWars2dRT-core/specific/model/planet_size.h"
#include "PlanetWars2dRT-core/utils/collision_2d_uint.h"


EXTERN_C_BEGIN


size_t
universe_do_collisions_of_ships_with_ships(universe_t* a_universe)
{
  size_t nb_collisions = 0;
  
  const size_t nb_ships = universe_get_nb_ships(a_universe);
  for(size_t i=0, j; i < nb_ships; ++i)
    {
      ship_t* ship_i = universe_get_ship_no_check(a_universe, i);
      
      for(j=i+1; j < nb_ships; ++j)
	{
	  ship_t* ship_j = universe_get_ship_no_check(a_universe, j);
	  if(collision_ship_with_other_player_ship(ship_i, ship_j))
	    {
	      ++nb_collisions;
	      
	      if(ship_is_destroyed(ship_i))
		{
		  break;
		}
	    }
	}
    }
  
  return nb_collisions;
}

static inline
bool
is_collision_ship_with_planet(const ship_t*   a_ship,
			      const planet_t* a_planet)
{
  const position_2d_uint ship_pos_top_left =
    ship_get_top_left_position_current(a_ship);
  const position_2d_uint planet_pos_center =
    planet_get_center_position(a_planet);
  return collision_2d_circle_rectangle_uint(&ship_pos_top_left,
					    &a_ship->size,
					    &planet_pos_center,
					    planet_get_radius(a_planet));
}

size_t
universe_do_collisions_of_ship_with_planets(universe_t* a_universe,
					    ship_t*     a_ship)
{
  size_t nb_collisions = 0;
  
  if(!ship_is_destroyed(a_ship))
    {
      const size_t nb_planets = universe_get_nb_planets(a_universe);
      for(size_t j=0; j < nb_planets; ++j)
	{
	  planet_t* planet_j = universe_get_planet_no_check(a_universe, j);
	  
	  if(is_collision_ship_with_planet(a_ship, planet_j) &&
	     planet_collision_with_not_destroyed_ship(planet_j, a_ship))
	    {
	      ++nb_collisions;
	      ship_destroy(a_ship);
	      break;
	    }
	}
    }
  
  return nb_collisions;
}

size_t
universe_do_collisions_of_ships_with_planets(universe_t* a_universe)
{
  size_t nb_collisions = 0;
  
  const size_t nb_ships = universe_get_nb_ships(a_universe);
  for(size_t i=0; i < nb_ships; ++i)
    {
      ship_t* ship_i = universe_get_ship_no_check(a_universe, i);
      nb_collisions +=
	universe_do_collisions_of_ship_with_planets(a_universe, ship_i);
    }
  
  return nb_collisions;
}


EXTERN_C_END
